package org.it3.Dominio;

import java.util.Vector;

import org.it3.Control.DBResource;
import org.it3.Control.ResourceType;
import org.it3.Control.InterfaceAgent;
import org.it3.Control.SingletonAgent;

public class AutorBDManager {
	
	 public boolean read(AutorBD autorBD) {
	    	try {
				InterfaceAgent agent = SingletonAgent.getAgent();
				
				String sql = "SELECT * FROM usuarios WHERE "
								+ "idusuarios = ?;";
				
				Vector<DBResource> resouceListIn = new Vector<DBResource>();
				resouceListIn.add( new DBResource(ResourceType.STRING, autorBD.getId()));
				
				Vector<DBResource> resouceListOut = new Vector<DBResource>();
				resouceListOut.add( new DBResource(ResourceType.INT) );
				resouceListOut.add( new DBResource(ResourceType.STRING) );

		    	
				Vector<Vector<Object>> res = agent.read( sql, resouceListIn, resouceListOut );
				
				boolean autenticado = res.size() > 0;
				if ( autenticado ) {
					autorBD.setId( (Integer) res.get(0).get(0) );
					autorBD.setNombre((String) res.get(0).get(1));
				}
				
				return autenticado;
	    	} catch ( Exception e) {
	    		int n = 11;
	    	}
	    	
	    	return false;
	    }
	 
	 public boolean readByName(AutorBD autorBD) {
	    	try {
				InterfaceAgent agent = SingletonAgent.getAgent();
				
				String sql = "SELECT * FROM usuarios WHERE "
								+ "username = ?;";
				
				Vector<DBResource> resouceListIn = new Vector<DBResource>();
				resouceListIn.add( new DBResource(ResourceType.STRING, autorBD.getNombre()));
				
				Vector<DBResource> resouceListOut = new Vector<DBResource>();
				resouceListOut.add( new DBResource(ResourceType.INT) );
				resouceListOut.add( new DBResource(ResourceType.STRING) );

		    	
				Vector<Vector<Object>> res = agent.read( sql, resouceListIn, resouceListOut );
				
				boolean autenticado = res.size() > 0;
				if ( autenticado ) {
					autorBD.setId( (Integer) res.get(0).get(0) );
					autorBD.setNombre((String) res.get(0).get(1));
				}
				
				return autenticado;
	    	} catch ( Exception e) {
	    		int n = 11;
	    	}
	    	
	    	return false;
	    }

}
